package entity.impl;

import entity.SudokuGrid;

/**
 * Created by kllso on 17-5-2.
 */
public class DefaultSudokuGridImpl implements SudokuGrid {
    private int[][] sudokuData = new int[9][9];

    public DefaultSudokuGridImpl() {
        for (int i = 0; i < 9; i++) {
            setRow(i, new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0});
        }
    }

    public DefaultSudokuGridImpl(int[][] grid) {
        setGrid(grid);
    }

    public int[] getRow(int rowIndex) {
        int[] row = new int[9];
        for (int x = 0; x < 9; x++) {
            row[x] = sudokuData[x][rowIndex];
        }
        return row;
    }

    public int[] getColumn(int columnIndex) {
        int[] column = new int[9];
        for (int y = 0; y < 9; y++) {
            column[y] = sudokuData[columnIndex][y];
        }
        return column;
    }

    public int[][] getBlock(int blockIndex) {
        int[][] block = new int[3][3];
        int firstBoxInBlockX = (blockIndex % 3) * 3;
        int firstBoxInBlockY = (blockIndex / 3) * 3;
        for (int y = 0; y < 3; y++) {
            for (int x = 0; x < 3; x++) {
                block[x][y] = sudokuData[firstBoxInBlockX + x][firstBoxInBlockY + y];
            }
        }
        return block;
    }

    public int getBox(int x, int y) {
        return sudokuData[x][y];

    }

    public int[][] getGrid() {
        int[][] result = new int[9][];
        for (int i = 0; i < 9; i++) {
            result[i] = sudokuData[i].clone();
        }
        return result;
    }

    public void setRow(int rowIndex, int[] row) {
        for (int x = 0; x < 9; x++) {
            sudokuData[x][rowIndex] = row[x];
        }

    }

    public void setColumn(int columnIndex, int[] column) {
        for (int y = 0; y < 9; y++) {
            sudokuData[columnIndex][y] = column[y];
        }
    }

    public void setBlock(int blockIndex, int[][] block) {
        int firstBoxInBlockX = (blockIndex % 3) * 3;
        int firstBoxInBlockY = (blockIndex / 3) * 3;
        for (int y = 0; y < 3; y++) {
            for (int x = 0; x < 3; x++) {
                sudokuData[firstBoxInBlockX + x][firstBoxInBlockY + y] = block[x][y];
            }
        }

    }

    public void setBox(int x, int y, int value) {
        sudokuData[x][y] = value;
    }

    public void setGrid(int[][] grid) {
        for (int i = 0; i < 9; i++) {
            sudokuData[i] = grid[i].clone();
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(260);
        sb.append("┌─────┬─────┬─────┐\n");
        for (int y = 0; y < 9; y++) {
            sb.append('│');
            for (int x = 0; x < 9; x++) {
                sb.append(sudokuData[x][y]);
                if (x % 3 == 2) {
                    sb.append('│');
                } else {
                    sb.append(' ');

                }
            }
            sb.append('\n');
            if (y % 3 == 2 && y != 8) {
                sb.append("├─────┼─────┼─────┤\n");
            }
        }
        sb.append("└─────┴─────┴─────┘\n");
        return sb.toString();
    }
}
